package com.hadwinling.blog.controller;

import com.hadwinling.blog.entity.Admin;
import com.hadwinling.blog.entity.Comment;
import com.hadwinling.blog.entity.Message;
import com.hadwinling.blog.service.CommentService;
import com.hadwinling.blog.service.MessageService;
import com.hadwinling.blog.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/23 20:12
 */
@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private MessageService messageService;

    @PostMapping("/comment")
    @ResponseBody
    public String comment(Comment comment,HttpServletRequest request){
//        System.out.println("执行插入评论操作");
//        System.out.println("comment："+comment);
        Admin user = (Admin) request.getSession().getAttribute("currUser");
        int userid = user.getId();
        comment.setUserid(userid);
        comment.setCreatetime(new Date());
        comment.setIsread(0);
        comment.setCommentcount(0);
        comment.setLikecount(0);
        int result = commentService.insertSelective(comment);
        //判断是给评论文章还是回复评论
        if (comment.getType() == 1){
            //去回复评论了
//            //更新回复数目
//            commentService.updateCommentcountByParentcommentid(comment.getId());

        }
        Message message = new Message();
        message.setAdminid(userid);
        message.setCommentid(comment.getId());
//            System.out.println("comment："+comment.getId());
        message.setIsread(0);
        message.setState(1);
        message.setCreatetime(new Date());
        messageService.insertSelective(message);
        if (result>0){
            return "1";
        }else {
            return "0";
        }
    }

    /*@GetMapping("/secondComment/{parentcommentid}")
    @ResponseBody
    public Msg secondComment(@PathVariable(name = "parentcommentid")int parentcommentid){
        List<Comment> commentList = commentService.selectAllByParentcommentidOrderByCreatetimeDesc(parentcommentid);

        return Msg.success().add("commentList",commentList);
    }*/

    @GetMapping("/secondComment")
    @ResponseBody
    public Msg secondComment(@RequestParam(name = "parentcommentid") int parentcommentid){
        List<Comment> commentList = commentService.selectAllByParentcommentidOrderByCreatetimeDesc(parentcommentid);

        return Msg.success().add("commentList",commentList);
    }


}
